package com.dongline.te
import javafx.application.Application
import javafx.stage.Stage
import org.apache.spark.{SparkConf, SparkContext}
class CustomSort extends  Application{

  /*
  使用自定义的case class最为key进行自定义排序，必须继承ordered类
   */
  override def start(primaryStage: Stage): Unit = {
    //创建驱动设置及context
    val conf: SparkConf =new SparkConf().setAppName("1").setMaster("local")
    //设置spark的内存
    conf.set("spark.testing.memory", "471859200")
    val sc: SparkContext =new SparkContext(conf)
    val rdd1 = sc.textFile("D://customorder.txt").map(line =>{
      line.split(",")
    })
    //创建自定义对象，用于键比较
    val rdd2 = rdd1.map( words =>{
      ( MyObj(words(0).toInt,words(1).toInt),words(2))
    })
    val rdd3 = rdd2.sortBy( x => x._1)
    print(rdd3.collect.toBuffer)
  }
}

//自定义比较对象，继承ordered，实现compare方法，如果大于就返回正，如果小于就返回负
case class MyObj(a:Int,b:Int) extends  Ordered[MyObj](){
  override def compare(that: MyObj): Int = {
    if(a>that.a)
      {
        return 1
      }
    else if(a==that.a)
      {
        if(b>that.b)
          {
            return 1
          }
        else if(b==that.b)
          {
            return 0
          }
      }
    return -1
  }
}
